package com.shop.cloud.controller.banner;

import io.swagger.annotations.ApiOperation;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.shop.cloud.common.mybatis.BaseController;
import com.shop.cloud.domain.hongfu.News;
import com.shop.cloud.domain.hongfu.Tag;
import com.shop.cloud.filter.CurrentUserId;
import com.shop.cloud.model.response.ObjectDataResponse;
import com.shop.cloud.service.hongfu.ILogService;
import com.shop.cloud.service.hongfu.INewsService;
import com.shop.cloud.service.hongfu.ITagService;
import com.shop.cloud.util.Page;

@RestController
@RequestMapping(value = "news")
public class NewsController extends BaseController {

	private static final Logger logger=Logger.getLogger(NewsController.class);
	
	@Autowired
	private INewsService newsService;
	
	@Autowired
	private ITagService tagService;
	
	@Autowired
	private ILogService logservice;
	
	
	@ApiOperation(value = "查询新闻" , notes="查询新闻")
	@RequestMapping(value = "queryNews", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Page<News>> queryNews(@CurrentUserId Long userId,@RequestBody News news){
		logservice.addLog(userId, 0, "进入查询新闻");
		news.setOrderBy("create_time desc");
		Page<News> datas=newsService.pageNews(news);
		return new ObjectDataResponse<Page<News>>(datas);
	}
	
	@ApiOperation(value = "查询标签" , notes="查询标签")
	@RequestMapping(value = "queryTags", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Page<Tag>> queryTags(@CurrentUserId Long userId,@RequestBody Tag tag){
		logservice.addLog(userId, 0, "进入查询标签");
//		tag.setOrderBy("create_time desc");
		Page<Tag> datas=tagService.page(tag);
		return new ObjectDataResponse<Page<Tag>>(datas);
	}
	
	@ApiOperation(value = "修改标签", notes = "修改标签")
	@RequestMapping(value = "editTags", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Integer> editTags(@CurrentUserId Long userId, @RequestBody Tag tag) {
		logservice.addLog(userId, 0, "进入修改标签");
		int row = 0;
		for(int i=0;i<tag.getTagIds().size();i++){
			tag.setId(tag.getTagIds().get(i));
			tag.setImgUrl(tag.getUrls().get(i));
			row =  tagService.update(tag);
		}

        return new ObjectDataResponse<Integer>(row);
	}
	
	@ApiOperation(value = "添加新闻", notes = "添加新闻")
	@RequestMapping(value = "addNews", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Boolean> addNews(@CurrentUserId Long userId, @RequestBody News news) {
		logservice.addLog(userId, 0, "进入添加新闻");
		news.setStatus(0);
		Boolean boo = newsService.insertNewsAndTag(news);
        return new ObjectDataResponse<Boolean>(boo);
	}
	
	@ApiOperation(value = "修改新闻", notes = "修改新闻")
	@RequestMapping(value = "editNews", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Integer> editNews(@CurrentUserId Long userId, @RequestBody News news) {
		logservice.addLog(userId, 0, "进入修改新闻");
		int row = newsService.updateNewsAndTag(news);
        return new ObjectDataResponse<Integer>(row);
	}
	
	@ApiOperation(value = "删除新闻", notes = "修改新闻")
	@RequestMapping(value = "deleteNews", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Integer> deleteNews(@CurrentUserId Long userId, @RequestBody News news) {
		logservice.addLog(userId, 0, "进入删除新闻");
		int row = newsService.deleteNewAndTag(news);
        return new ObjectDataResponse<Integer>(row);
	}
	
	
	
	
}